Find length of the Longest Prefix which is also Suffix¶
Returns length of the longest prefix which is also suffix
and the two do not overlap. This function mainly is a copy
of computeLPSArray(). See:
https://www.geeksforgeeks.org/searching-for-patterns-set-2-kmp-algorithm/
def longestPrefixSuffix(s):
n = len(s)
for res in range(n // 2, 0, -1):
# Check for shorter lengths of first half
prefix = s[0: res]
suffix = s[n - res: n]
if (prefix == suffix):
return res
# if no prefix and suffix match occurs
return 0
def longestPrefixSuffix(s):
n = len(s)
lps = [0] * n # lps[0] is always 0
l = 0 # length of the previous longest prefix suffix
# the loop calculates lps[i] for i = 1 to n-1
i = 1
while (i < n):
if (s[i] == s[l]):
l = l + 1
lps[i] = l
i = i + 1
else:
# (pat[i] != pat[len])
# This is tricky. Consider the example:
# AAACAAAA and i = 7.
# The idea is similar to search step.
if (l != 0):
l = lps[l - 1]
# Also, note that we do not increment i here
else:
# if (len == 0)
lps[i] = 0
i = i + 1
res = lps[n - 1]
# Since we are looking for non overlapping parts.
if (res > n / 2):
return n // 2
else:
return res
Test¶
txt = "AABAACAADAABAABA"
print(longestPrefixSuffix(txt)) # 4